package com.hankz.cc.leetcode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @auther zzc
 * @date 2021/3/19 14:46
 */
public class 有效的括号 {

    private static final Map<Character, Character> map = new HashMap<Character, Character>() {
        {
            put('[', ']');
            put('{', '}');
            put('(', ')');
            put('?', '?');
        }
    };

    public static void main(String[] args) {
        String a = "{{{}}}";
        boolean vaild = isVaild(a);
        System.out.println(vaild);
    }

    private static boolean isVaild(String a) {
        if (a.length() > 0 && !map.containsKey(a.charAt(0))) return false;
        LinkedList<Character> stack = new LinkedList<Character>() {{
            add('?');
        }};
        for (Character c : a.toCharArray()) {
            if (map.containsKey(c)) {
                stack.add(c);
            } else if (map.get(stack.removeLast()) != c) {
                return false;
            }
        }
        return stack.size()==1;
    }
}
